package com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.dto.ErpFinancepaymentDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.dto.SaveErpFinancepaymentFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.vo.ErpFinancepaymentFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.vo.ErpFinancepaymentListVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.dto.ErpFinancepaymentListDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.vo.ErpFinancepaymentbillVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.entity.ErpFinancepaymentbill;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.dto.ErpFinancepaymentbillDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.entity.ErpFinancepayment;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.vo.ErpFinancepaymentVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinancePayment.service.IErpFinancepaymentService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_financepayment")
@Api(value = "付款管理", tags = "付款管理接口")
public class ErpFinancepaymentController {


	private final IErpFinancepaymentService erpFinancepaymentService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpFinancepayment")
	public Response<ErpFinancepaymentFormDataVo> getFormData(@PathVariable String id) {
		ErpFinancepaymentFormDataVo formDataVo = new ErpFinancepaymentFormDataVo();
		// 主表数据
		ErpFinancepayment erpFinancepayment = erpFinancepaymentService.getById(id);
		formDataVo.setErpFinancepaymentVo(BeanUtil.copy(erpFinancepayment, ErpFinancepaymentVo.class));
		// 从表数据
		formDataVo.setErpFinancepaymentbillVoList(BeanUtil.copyList(erpFinancepaymentService.getErpFinancepaymentbillByParentId(id), ErpFinancepaymentbillVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpFinancepayment")
	public Response<PageOutput<ErpFinancepaymentListVo>> getPageList(ErpFinancepaymentListDto listDto) {
		IPage<ErpFinancepayment> page = erpFinancepaymentService.getPageList(listDto);
		List<ErpFinancepaymentListVo> records = BeanUtil.copyList(page.getRecords(), ErpFinancepaymentListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpFinancepayment")
	public Response save(@RequestBody SaveErpFinancepaymentFormDataDto formDto) {
		ErpFinancepaymentDto erpFinancepaymentDto = formDto.getErpFinancepaymentDto();
		ErpFinancepayment erpFinancepayment = BeanUtil.copy(erpFinancepaymentDto, ErpFinancepayment.class);
		List<ErpFinancepaymentbillDto> erpFinancepaymentbillDtoList = formDto.getErpFinancepaymentbillDto();
		List<ErpFinancepaymentbill> erpFinancepaymentbillList = BeanUtil.copyList(erpFinancepaymentbillDtoList, ErpFinancepaymentbill.class);
		String dataId = erpFinancepaymentService.addErpFinancepayment(erpFinancepayment, erpFinancepaymentbillList);
		if (dataId != null) {
			codeRuleService.useEncode("付款编码");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpFinancepayment")
	public Response update(@PathVariable String id, @RequestBody SaveErpFinancepaymentFormDataDto formDto) {
		ErpFinancepaymentDto erpFinancepaymentDto = formDto.getErpFinancepaymentDto();
		ErpFinancepayment erpFinancepayment = BeanUtil.copy(erpFinancepaymentDto, ErpFinancepayment.class);

		List<ErpFinancepaymentbillDto> erpFinancepaymentbillDtoList = formDto.getErpFinancepaymentbillDto();
		List<ErpFinancepaymentbill> erpFinancepaymentbillList = BeanUtil.copyList(erpFinancepaymentbillDtoList, ErpFinancepaymentbill.class);
		return Response.status(erpFinancepaymentService.updateErpFinancepayment(id, erpFinancepayment, erpFinancepaymentbillList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpFinancepaymentService.removeByIds(Func.toStrList(ids)));
	}

	
}
